//---------------------------------------------------------------------------


#pragma hdrstop

#include "TConnectionListener.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

TConnListener::~TConnListener(){
	try{
		DisConnect();

    }catch(...){}
}

TConnListener::TConnListener(std::string host){
	EnableRoster=true;
	EnableLogger=false;
	client=new Client(host);
}

void TConnListener::onConnect(){
   if(FOnConnect)FOnConnect(NULL);
}

void TConnListener::onDisconnect(ConnectionError e){
   if(FOnDisConnect)FOnDisConnect(e);
}

bool TConnListener::onTLSConnect(const CertInfo &info){
   if(FOnTLSConnect)return FOnTLSConnect(info);
}

void __fastcall TConnListener::Connect(){
	client->registerConnectionListener(this);
	if(EnableLogger){
		logger=new TLogHandler(client);
		client->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, logger );
		logger->FOnLog=this->FOnLog;
	}
	if(EnableRoster){
		client->setUsername(UserName);
		client->setPassword(PassWord);
		client->setPort(Port);
		client->setResource(Resource);
		client->disco()->setIdentity("client","bot");
		client->disco()->setVersion("WYHXMPP",GLOOX_VERSION);
	}
	//
// this code connects to a jabber server through a SOCKS5 proxy
//
//       ConnectionSOCKS5Proxy* conn = new ConnectionSOCKS5Proxy( j,
//                                   new ConnectionTCP( j->logInstance(),
//                                                      "sockshost", 1080 ),
//                                   j->logInstance(), "example.net" );
//       conn->setProxyAuth( "socksuser", "sockspwd" );
//       j->setConnectionImpl( conn );

//
// this code connects to a jabber server through a HTTP proxy through a SOCKS5 proxy
//
//       ConnectionTCP* conn0 = new ConnectionTCP( j->logInstance(), "old", 1080 );
//       ConnectionSOCKS5Proxy* conn1 = new ConnectionSOCKS5Proxy( conn0, j->logInstance(), "old", 8080 );
//       conn1->setProxyAuth( "socksuser", "sockspwd" );
//       ConnectionHTTPProxy* conn2 = new ConnectionHTTPProxy( j, conn1, j->logInstance(), "jabber.cc" );
//       conn2->setProxyAuth( "httpuser", "httppwd" );
//       j->setConnectionImpl( conn2 );

	recver=new TRecvThread(false,client);
}

void __fastcall TConnListener::DisConnect()
{
	try{
        if(NULL!=client&&StateDisconnected!=client->state()) client->disconnect();
		if(NULL!=recver){
			recver->Terminate();
			delete recver;
		}


	}catch(...){};

}



